package com.whq.thrift.api.web.admin.controller.api;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.baseconf.ModuleInfo;
import com.whq.thrift.api.web.admin.db.methodinfo.MethodInfo;
import com.whq.thrift.api.web.admin.db.urlmapping.UrlMappingInfo;
import com.whq.thrift.api.web.admin.service.baseconf.ModuleInfoService;
import com.whq.thrift.api.web.admin.service.baseconf.UrlMappingInfoService;
import com.whq.thrift.api.web.admin.service.verify.MethodInfoService;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class ApiIndexController {
	private static final String URL = "http://api.w.sdo.com";
	private static final String TOB_URL = "http://txz2b.sdo.com";

	@Autowired
	private MethodInfoService methodInfoService;
	@Autowired
	private ModuleInfoService moduleInfoService;
	@Autowired
	private UrlMappingInfoService urlMappingInfoService;

	@RequestMapping(value = "api/index/list")
	public String list(@RequestParam(value = "introFunc", required = false) String introFunc,
			@RequestParam(value = "methodName", required = false) String methodName, @RequestParam(value = "url", required = false) String url, Model model) {
		PageData<MethodInfo> pageData = new PageData<MethodInfo>();

		Map<String, Object> urlWhereParams = new HashMap<String, Object>(0);
		urlWhereParams.clear();
		urlWhereParams.put("url", "%");
		List<UrlMappingInfo> allUrlMappingApiList = this.urlMappingInfoService.findUrlMappingApiList(urlWhereParams);
		Map<String, String> allMethodUrlMap = new HashMap<String, String>(0);
		if (allUrlMappingApiList != null) {
			for (Iterator<UrlMappingInfo> iterator = allUrlMappingApiList.iterator(); iterator.hasNext();) {
				UrlMappingInfo urlMappingInfo = (UrlMappingInfo) iterator.next();
				if (urlMappingInfo != null) {
					allMethodUrlMap.put(urlMappingInfo.getMapParamMethod(), urlMappingInfo.getUrl());
				}
			}
		}

		List<UrlMappingInfo> findUrlMappingApiList = null;
		if (StringUtil.isNotBlank(url)) {
			urlWhereParams.clear();
			urlWhereParams.put("url", "%" + url.trim() + "%");

			findUrlMappingApiList = this.urlMappingInfoService.findUrlMappingApiList(urlWhereParams);
		}

		// 查询moduleinfo的信息，与方法信息中的数据比对
		List<ModuleInfo> moduleInfoList = this.moduleInfoService.findForGatewayList();
		Set<String> moduleIdSet = new HashSet<String>(0);
		for (Iterator<ModuleInfo> iterator = moduleInfoList.iterator(); iterator.hasNext();) {
			ModuleInfo oneModuleInfo = (ModuleInfo) iterator.next();
			if (oneModuleInfo != null) {
				moduleIdSet.add(oneModuleInfo.getModuleId());
			}
		}

		// url映射
		Set<String> urlMethodSet = new HashSet<String>(0);

		if (findUrlMappingApiList != null && findUrlMappingApiList.size() > 0) {
			for (Iterator<UrlMappingInfo> iterator = findUrlMappingApiList.iterator(); iterator.hasNext();) {
				UrlMappingInfo urlInfo = (UrlMappingInfo) iterator.next();
				if (urlInfo != null) {
					urlMethodSet.add(urlInfo.getMapParamMethod());
				}
			}
		}

		// 查询参数
		Map<String, Object> whereParams = new HashMap<String, Object>(0);

		// 查询参数
		if (StringUtil.isNotBlank(introFunc)) {
			whereParams.put("introFunc", "%" + introFunc.trim() + "%");
		}
		if (StringUtil.isNotBlank(methodName)) {
			whereParams.put("methodName", "%" + methodName.trim() + "%");
		}
		List<MethodInfo> resultList = methodInfoService.findApiInfoList(whereParams);
		for (Iterator<MethodInfo> iterator = resultList.iterator(); iterator.hasNext();) {
			MethodInfo oneMethod = (MethodInfo) iterator.next();
			if (oneMethod == null) {
				continue;
			}

			// 判断方法是否需要显示
			String oneMethodName = oneMethod.getMethodName();
			String methodModuleId = "";
			if (StringUtil.isNotBlank(oneMethodName)) {
				List<String> split2list = StringUtil.split2list(oneMethodName, "\\.");
				if (!split2list.isEmpty()) {
					methodModuleId = split2list.get(0);
				}
			}
			if (!moduleIdSet.contains(methodModuleId)) {
				iterator.remove();
				continue;
			}

			// 根据url进行过滤
			if (StringUtil.isNotBlank(url)) {
				if (!urlMethodSet.contains(oneMethodName)) {
					iterator.remove();
					continue;
				}
			}

			String methodUrl = allMethodUrlMap.get(oneMethodName);
			if (StringUtil.isBlank(methodUrl)) {
				methodUrl = URL + "/apipool?method=" + oneMethodName;
			} else {
				if (methodUrl.contains("/tob/txz_bs_mixed")) {
					methodUrl = TOB_URL + methodUrl;
				} else {
					methodUrl = URL + methodUrl;
				}
			}
			oneMethod.setUrl(methodUrl);
		}

		pageData.setResultList(resultList);

		model.addAttribute("introFunc", introFunc);
		model.addAttribute("url", url);
		model.addAttribute("methodName", methodName);
		model.addAttribute("pageData", pageData);
		return "api/index/list";
	}

	@RequestMapping(value = "api/index/show")
	public String show(@RequestParam(value = "id", required = true) Integer id, Model model) {

		MethodInfo method = this.methodInfoService.getMethodById(id);
		if (method == null) {
			model.addAttribute("errMsg", "缺少参数: 找不到方法!");
			return "error";
		}

		Map<String, Object> whereParams = new HashMap<String, Object>(0);
		if (StringUtil.isNotBlank(method.getMethodName())) {
			whereParams.put("mapParamMethod", method.getMethodName());
		}

		String methodUrl = URL + "/apipool?method=" + method.getMethodName();

		List<UrlMappingInfo> findUrlMappingApiList = this.urlMappingInfoService.findUrlMappingApiList(whereParams);
		if (findUrlMappingApiList != null && findUrlMappingApiList.size() > 0) {
			UrlMappingInfo urlMappingInfo = findUrlMappingApiList.get(0);
			if (urlMappingInfo.getMapParamMethod().equals(method.getMethodName())) {
				String introRequestParams = method.getIntroRequestParams();
				if (introRequestParams.contains(method.getMethodName())) {
					int methodIndex = introRequestParams.indexOf("method");
					int countLength = introRequestParams.indexOf(method.getMethodName()) + method.getMethodName().length();
					String subSequence = introRequestParams.subSequence(methodIndex, countLength).toString();
					method.setIntroRequestParams(introRequestParams.replace("\r\n" + subSequence, ""));
				}
				if (urlMappingInfo.getUrl().contains("/tob/txz_bs_mixed")) {
					methodUrl = TOB_URL + urlMappingInfo.getUrl();
				} else {
					methodUrl = URL + urlMappingInfo.getUrl();
				}
			}
		}

		method.setUrl(methodUrl);
		model.addAttribute("method", method);
		return "api/index/show";
	}
}
